package com.cloudeye.business.mq.service;

import com.cloudeye.business.mq.config.RabbitMqMsgTypeEnum;
import com.cloudeye.business.mq.model.MqMsg;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;

/**
 * @Author: lafangyuan
 * @Date: 2021/12/6 13:34
 */
public class MqMsgSenderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String send(RabbitMqMsgTypeEnum type, MqMsg msg){
        MessageProperties properties = new MessageProperties();
        properties.setMessageId(msg.getMsgId());
        properties.setTimestamp(msg.getSendTime());
        Message message = MessageBuilder.withBody(msg.getBody().getBytes(StandardCharsets.UTF_8))
                .andProperties(properties).build();
        rabbitTemplate.send(type.getExchange(),type.getRoutingKey(),message);
        return msg.getMsgId();
    }
}
